<?php
/*
---------------------------------------------------------------------------
status/queue_data.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require_once("../../library/frontend.php");

$total_triggers = 0;
$run_result = run_query("SELECT frequency, last_run FROM run_times ORDER BY frequency ASC");
if (mysql_num_rows($run_result) != NULL) {
	while(list($frequency, $last_run) = mysql_fetch_array($run_result))	{
		$trigger_result = run_query("SELECT id, name FROM triggers WHERE frequency = '$frequency' ORDER BY name ASC");
		if (mysql_num_rows($trigger_result) != NULL) {
			$firstrow = TRUE;
			while(list($trigger_id, $trigger_name) = mysql_fetch_array($trigger_result))	{
				$host_result = run_query("SELECT hosts.name FROM hosts, hosts_triggers WHERE hosts_triggers.trigger_id = '$trigger_id' AND hosts_triggers.host_id = hosts.id AND hosts.enable_monitoring = 1 ORDER BY hosts.name ASC");
				if (mysql_num_rows($host_result) != NULL) {
					$total_triggers++;
					if ($firstrow == TRUE) {
						echo '<h3>', $frequency, ' Minute Triggers:</h3>
						<table width="800px">
						<tr><th>Trigger:</th><th>On Hosts:</th><th>Next Scheduled Run</th></tr>';
						$firstrow == FALSE;
					}
					echo '<tr><td width="150px">', $trigger_name, '</td><td>';
					$firstrow = TRUE;
					while(list($host_name) = mysql_fetch_array($host_result)){
						if ($firstrow == TRUE) {
							echo $host_name;
							$firstrow = FALSE;
						}
						else
							echo ', ', $host_name;
					}
					$next_run = ($last_run + ($frequency * 60));
					echo '</td><td width="175px">', date("D M j G:i:s T Y", $next_run), '</td></tr>';
				}
			}
			echo '</table>';
		}
	}
}
if ($total_triggers == 0)
	echo '<h3>Nothing in the Queue...If this is a new install each cron job must run once before the queue will populate.</h3>Still having problems?<ul><li>Make sure you have hosts assigned to triggers</li><li>Make sure your cron jobs were setup correctly.</li></ul>';
echo '<div class="current_time">Current Date/Time: ', date("D M j G:i:s T Y"), '</div>';
?>